#include "../stdafx.h"

#include <boost/lexical_cast.hpp>

#include "AutoLock.h"

ProcessLock::ProcessLock(wstring key)
{
	event = CreateEvent(0, FALSE, TRUE, key.c_str());
	assert(event);
	WaitForSingleObject(event, INFINITE);

}

ProcessLock::~ProcessLock(void)
{
	SetEvent(event);
	CloseHandle(event);
}

wstring GetProcessIDStr(){
	DWORD id = GetCurrentProcessId();
	return boost::lexical_cast<wstring>(id);
}

ThreadLock::ThreadLock(wstring key):ProcessLock(GetProcessIDStr() + key){}

static CRITICAL_SECTION section = {0};
static bool sectionInit = false;

AutoLock::AutoLock(){
	if(!sectionInit){
		::InitializeCriticalSection(&section);
		sectionInit = true;
	}
	::EnterCriticalSection(&section);
}
AutoLock::~AutoLock(){
	::LeaveCriticalSection(&section);
}
